Skip to content

Don't fail a URLSessionTask on a 401 response #2255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

finestructure
Copy link

Cherry-picked from master, for inclusion in 5.0.2

Original MR description:

--

The basic authentication implementation fails a URLSessionTask with an error if
a status code of 401 is received but we aren't able to build a
URLProtectionSpace, possibly due to the absence of a the WWW-Authenticate
response header. A valid HTTP response object is received in this case. It may
not possible to go ahead with the urlSession(_,task: task, didReceive:)
callback, either because a delegate isn't being used or because the
WWW-Authenticate response header is absent. In such cases, the completion
handler is invoked with the received response, NOT with an error.

Note that basic authentication
(4c1e8c4271c4e21000528
d3910f20a958d03f308#diff-14c6cb724eead661596e4468ae5ca638) wasn't available
with Swift 4.x. It was first made available in development builds for Swift 5.
Initially, when we failed to build a URLProtectionSpace, we failed with an
error, which resulted in removing the task from the TaskRegistry. Further we
went ahead and invoked the completion handler, which again tried to remove the
same task from the TaskRegistry, resulting in a fatal error reported against
Swift 5.0. As a fix to this, we decided to simply return after failing with an
error (see
69abd6dd396e1985a882a9
a39079801f84658fb3#diff-14c6cb724eead661596e4468ae5ca638) which means we
skipped invoking the completion handler.

It seems that the right thing to do is NOT fail with an error, but simply
invoke the completion handler with the 401 response received.

The basic authentication implementation fails a URLSessionTask with an error if
a status code of 401 is received but we aren't able to build a
URLProtectionSpace, possibly due to the absence of a the WWW-Authenticate
response header. A valid HTTP response object is received in this case. It may
not possible to go ahead with the `urlSession(_,task: task, didReceive:)`
callback, either because a delegate isn't being used or because the
`WWW-Authenticate` response header is absent. In such cases, the completion
handler is invoked with the received response, NOT with an error.

Note that basic authentication
(swiftlang@4c1e8c4271c4e21000528
d3910f20a958d03f308#diff-14c6cb724eead661596e4468ae5ca638) wasn't available
with Swift 4.x. It was first made available in development builds for Swift 5.
Initially, when we failed to build a URLProtectionSpace, we failed with an
error, which resulted in removing the task from the TaskRegistry. Further we
went ahead and invoked the completion handler, which again tried to remove the
same task from the TaskRegistry, resulting in a fatal error reported against
Swift 5.0. As a fix to this, we decided to simply return after failing with an
error (see
swiftlang@69abd6dd396e1985a882a9
a39079801f84658fb3#diff-14c6cb724eead661596e4468ae5ca638) which means we
skipped invoking the completion handler.

It seems that the right thing to do is NOT fail with an error, but simply
invoke the completion handler with the 401 response received.
@millenomi
Copy link
Contributor

@shahmishal Merging in swift-5.0-branch for monthlies seems to be disabled, is that intentional?

@shahmishal
Copy link
Member

@swift-ci test

@millenomi millenomi merged commit 0f28ef1 into swiftlang:swift-5.0-branch May 15, 2019
@finestructure
Copy link
Author

I was hoping to test this out with a snapshot but it looks like there aren't any for the swift-5.0-branch at the moment. I see there have been swift-5.0-DEVELOPMENT-SNAPSHOT-xxx tags in the past (last one from March 24).

Would it be possible to trigger a snapshot for the swift-5.0-branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants